/**
 * 
 */
package com.gfwy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.gfwy.dao.IAdminDao;
import com.gfwy.vo.Admin;

/**
 * @author Administrator
 *
 */
public class AdminDaoImpl implements IAdminDao {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	
	public AdminDaoImpl(Connection conn){
		this.conn = conn;
	}
	@Override
	public boolean doCreate(Admin admin) throws Exception {
		boolean flag = false;
		try {
			String sql = "INSERT INTO admin(aid,password) VALUES (?,?)";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1,admin.getAid());
			this.pstmt.setString(2,admin.getPassword());
			if(this.pstmt.executeUpdate()>0){
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally{
			if(this.pstmt != null){
				try{
					this.pstmt.close();
				}catch(Exception e){
					throw e;
				}
			}
		}
		return flag;
	}
	@Override
	public Admin findById(String aid) throws Exception {
		Admin admin = null;
		try {
			String sql = "SELECT * FROM admin WHERE aid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1,aid);
			this.rs = this.pstmt.executeQuery();
			if(this.rs.next()){
				admin = new Admin();
				admin.setAid(rs.getString("aid"));
				admin.setPassword(rs.getString("password"));
			}
		} catch (Exception e) {
			throw e;
		} finally{
			if(this.rs != null){
				try{
					this.rs.close();
				}catch(Exception e){
					throw e;
				}
			}
			if(this.pstmt != null){
				try{
					this.pstmt.close();
				}catch(Exception e){
					throw e;
				}
			}
		}
		return admin;
	}
	@Override
	public boolean doUpdatePwd(Admin admin) throws Exception {
		boolean flag = false;
		try {
			String sql = "UPDATE admin SET password=? WHERE aid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, admin.getPassword());
			this.pstmt.setString(2, admin.getAid());
			if(this.pstmt.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally{
			if(this.pstmt != null){
				try{
					this.pstmt.close();
				}catch(Exception e){
					throw e;
				}
			}
		}
		return flag;
	}

}
